home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 41 / Amiga Format CD41 (1999-06)(Future Publishing)(GB)[!][issue 1999-07].iso / -seriously_amiga- / programming / other / ppc_c2p / howtouse.c next >
C/C++ Source or Header  |  1999-04-19  |  6KB  |  268 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #include <exec/types.h>
  7.  
  8. #include <proto/graphics.h>
  9. #include <proto/intuition.h>
  10. #include <proto/utility.h>
  11. #include <proto/va3d.h>
  12.  
  13. #include "ppc_c2p.h"
  14.  
  15.  
  16. #define TAG_ITEM(tagitem, tag, data) tagitem.ti_Tag=(ULONG)tag;tagitem.ti_Data=(ULONG)data;
  17.  
  18.  
  19. typedef
  20.     struct Display
  21.     {
  22.         ULONG Width, Height;
  23.         ULONG Type;
  24.  
  25.         ULONG Depth;
  26.         ULONG DisplayID;
  27.  
  28.         struct Screen *Screen;
  29.         struct ViewPort *ViewPort;
  30.         struct Window *Window;
  31.         struct RastPort *RastPort;
  32.         struct BitMap *BitMap;
  33.  
  34.     } DISPLAY;
  35.  
  36. #define DT_NORMAL8              0
  37. #define DT_HAM6            1
  38.  
  39. DISPLAY *MainDisplay;
  40.  
  41. UWORD SGIheader[256]; 
  42. UBYTE *R, *G, *B;
  43. UBYTE *chunky;
  44. UWORD *rgb15;
  45. ULONG palette[256*3+2];
  46.  
  47. DISPLAY *OpenDisplay(ULONG Width, ULONG Height, ULONG Type)
  48. {
  49. DISPLAY *Display;
  50. struct TagItem tmpTagList[20];
  51. LONG i, j;
  52. ULONG *Plane4, *Plane5;
  53. ULONG DWidth;
  54.  
  55.     if((Display = calloc(1, sizeof(DISPLAY))) == NULL)
  56.         return NULL;
  57.  
  58.     Display->Width = Width;
  59.     Display->Height = Height;
  60.     Display->Type = Type;
  61.  
  62.     switch(Type)
  63.     {
  64.         case DT_NORMAL8:
  65.             Display->Depth = 8;
  66.             DWidth = Width;
  67.             TAG_ITEM(tmpTagList[0], BIDTAG_NominalWidth, DWidth);
  68.             TAG_ITEM(tmpTagList[1], BIDTAG_NominalHeight, Height);
  69.             TAG_ITEM(tmpTagList[2], BIDTAG_Depth, Display->Depth);
  70.             TAG_ITEM(tmpTagList[3], TAG_DONE, 0);
  71.             break;
  72.  
  73.         case DT_HAM6:
  74.             Display->Depth = 6;
  75.             DWidth = Width*4;
  76.             TAG_ITEM(tmpTagList[0], BIDTAG_NominalWidth, DWidth);
  77.             TAG_ITEM(tmpTagList[1], BIDTAG_NominalHeight, Height);
  78.             TAG_ITEM(tmpTagList[2], BIDTAG_Depth, Display->Depth);
  79.             TAG_ITEM(tmpTagList[3], BIDTAG_DIPFMustHave, DIPF_IS_HAM);
  80.             TAG_ITEM(tmpTagList[4], TAG_DONE, 0);
  81.             break;
  82.     }
  83.  
  84.     if((Display->DisplayID = BestModeIDA(tmpTagList)) == (ULONG)INVALID_ID)
  85.         return NULL;
  86.  
  87.     TAG_ITEM(tmpTagList[0], SA_Width, DWidth);
  88.     TAG_ITEM(tmpTagList[1], SA_Height, Display->Height);
  89.     TAG_ITEM(tmpTagList[2], SA_Depth, Display->Depth);
  90.     TAG_ITEM(tmpTagList[3], SA_Quiet, TRUE);
  91.     TAG_ITEM(tmpTagList[4], SA_Type, CUSTOMSCREEN);
  92.     TAG_ITEM(tmpTagList[5], SA_DisplayID, Display->DisplayID);
  93.     TAG_ITEM(tmpTagList[6], SA_AutoScroll, FALSE);
  94.     TAG_ITEM(tmpTagList[7], SA_ShowTitle, FALSE);
  95.     TAG_ITEM(tmpTagList[8], SA_Interleaved, FALSE);
  96.     TAG_ITEM(tmpTagList[9], TAG_DONE, 0);
  97.  
  98.     if((Display->Screen = OpenScreenTagList(NULL, tmpTagList)) == NULL)
  99.         return NULL;
  100.  
  101.     Display->ViewPort = &Display->Screen->ViewPort;
  102.  
  103.     TAG_ITEM(tmpTagList[0], WA_Left, 0);
  104.     TAG_ITEM(tmpTagList[1], WA_Top, 0);
  105.     TAG_ITEM(tmpTagList[2], WA_Width, DWidth);
  106.     TAG_ITEM(tmpTagList[3], WA_Height, Display->Height);
  107.     TAG_ITEM(tmpTagList[4], WA_IDCMP, 0);
  108.     TAG_ITEM(tmpTagList[5], WA_CustomScreen, Display->Screen);
  109.     TAG_ITEM(tmpTagList[6], WA_Backdrop, TRUE);
  110.     TAG_ITEM(tmpTagList[7], WA_Borderless, TRUE);
  111.     TAG_ITEM(tmpTagList[8], WA_Activate, TRUE);
  112.     TAG_ITEM(tmpTagList[9], WA_RMBTrap, TRUE);
  113.     TAG_ITEM(tmpTagList[10], WA_NoCareRefresh, TRUE);
  114.     TAG_ITEM(tmpTagList[11], TAG_DONE, 0);
  115.     if((Display->Window = OpenWindowTagList(NULL, tmpTagList)) == NULL)
  116.         return NULL;
  117.  
  118.     if((Display->RastPort = malloc(sizeof(struct RastPort))) == NULL)
  119.         return NULL;
  120.  
  121.     memcpy(Display->RastPort, Display->Window->RPort, sizeof(struct RastPort));
  122.  
  123.     Display->BitMap = Display->RastPort->BitMap;
  124.  
  125.     switch(Type)
  126.     {
  127.         case DT_HAM6:
  128.  
  129.             Plane4 = (ULONG *)Display->BitMap->Planes[4];
  130.             Plane5 = (ULONG *)Display->BitMap->Planes[5];
  131.  
  132.           for(i=0; i<Height; i++)
  133.                 for(j=0; j<(DWidth/32); j++)
  134.               {    // pixel order: RGGB
  135.                 Plane4[i*DWidth/32+j] = 0x77777777;
  136.                 Plane5[i*DWidth/32+j] = 0xeeeeeeee;
  137.                  }
  138.  
  139.             break;
  140.  
  141.           default:
  142.             break;
  143.     }
  144.  
  145.     return Display;
  146. }
  147.  
  148.  
  149. VOID CloseDisplay(DISPLAY *Display)
  150. {
  151.  
  152.     if(Display)
  153.     {
  154.         if(Display->RastPort)
  155.         {
  156.             free(Display->RastPort);
  157.             Display->RastPort = NULL;
  158.         }
  159.  
  160.         if(Display->Window)
  161.         {
  162.             CloseWindow(Display->Window);
  163.             Display->Window = NULL;
  164.         }
  165.  
  166.         if(Display->Screen)
  167.         {
  168.             CloseScreen(Display->Screen);
  169.             Display->Screen = NULL;
  170.         }
  171.  
  172.         free(Display);
  173.     }
  174. }
  175.  
  176. #define WIDTH 320
  177. #define HEIGHT 200
  178.  
  179. #define TEST_IMAGE15 "data/test_image.sgi"
  180. #define TEST_IMAGE8 "data/test_image.chunky"
  181. #define TEST_IMAGE8_PALETTE "data/test_image.palette"
  182.  
  183. #define FRAMES 100
  184.  
  185. #define TEST(FUNC, src)    \
  186.     printf("-------------------------------------------------------------\n");\
  187.     printf(#FUNC "()\n");\
  188.     PPCCacheFlushAll();\
  189.     StartTime = clock();\
  190.     for(i=0; i<FRAMES; i++)\
  191.     {\
  192.         FUNC ## (src, (ULONG **)MainDisplay->BitMap->Planes, WIDTH, HEIGHT);\
  193.     }\
  194.     StopTime = clock();\
  195.     printf("total time: %f sec\n", (FLOAT)(StopTime-StartTime)/(FLOAT)CLK_TCK);\
  196.     printf("fps: %f\n", (FLOAT)FRAMES*(FLOAT)CLK_TCK/(FLOAT)(StopTime-StartTime));
  197.  
  198.  
  199. VOID main(VOID)
  200. {
  201. FILE *File;
  202. ULONG width, height;
  203. LONG i;
  204. clock_t StartTime;
  205. clock_t StopTime;
  206.  
  207. // load test image
  208.  
  209.     File = fopen(TEST_IMAGE15, "rb");
  210.  
  211.     fread(SGIheader, 256, 2, File);
  212.  
  213.     width = SGIheader[3];
  214.     height = SGIheader[4];
  215.  
  216.     R = malloc(width*height);
  217.     G = malloc(width*height);
  218.     B = malloc(width*height);
  219.     chunky = malloc(width*height+32);
  220.     chunky = (UBYTE *)(((ULONG)chunky+31)&0xffffffe0);
  221.  
  222.     fread(chunky, width, height, File);
  223.     for(i=0; i<height; i++)
  224.         memcpy(&R[i*width], &chunky[(height-i-1)*width], width);
  225.  
  226.     fread(chunky, width, height, File);
  227.     for(i=0; i<height; i++)
  228.         memcpy(&G[i*width], &chunky[(height-i-1)*width], width);
  229.  
  230.     fread(chunky, width, height, File);
  231.     for(i=0; i<height; i++)
  232.         memcpy(&B[i*width], &chunky[(height-i-1)*width], width);
  233.  
  234.     fclose(File);
  235.  
  236.     rgb15 = malloc(width*height*2+32);
  237.     rgb15 = (UWORD *)(((ULONG)rgb15+31)&0xffffffe0);
  238.  
  239.     for(i=0; i<width*height; i++)
  240.         rgb15[i] = ((R[i]&0xf8)<<(10-3))+((G[i]&0xf8)<<(5-3))+((B[i]&0xf8)>>3);
  241.  
  242. //
  243.  
  244.     File = fopen(TEST_IMAGE8, "rb");
  245.     fread(chunky, width, height, File);
  246.     fclose(File);
  247.  
  248.     File = fopen(TEST_IMAGE8_PALETTE, "rb");
  249.     fread(palette, 256*3+2, 4, File);
  250.     fclose(File);
  251.  
  252.  
  253. // test rgb15 to ham6 c2p
  254.  
  255.     MainDisplay = OpenDisplay(WIDTH, HEIGHT, DT_HAM6);
  256.     TEST(RGB15_TO_HAM6_NI, rgb15);
  257.     CloseDisplay(MainDisplay);
  258.  
  259. // test c2p
  260.  
  261.     MainDisplay = OpenDisplay(WIDTH, HEIGHT, DT_NORMAL8);
  262.     LoadRGB32(MainDisplay->ViewPort, palette);
  263.     TEST(C2P_NI, chunky);
  264.     CloseDisplay(MainDisplay);
  265.  
  266.     exit(0);
  267. }
  268.